package com.anrongtec.zcpt.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.anrongtec.zcpt.R;
import com.anrongtec.zcpt.module.AppModel;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/6/7.
 */

public class EssenceRecycleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {
    private List<AppModel> mData = new ArrayList<>();

    private Context mContext;


    public void setData(List<AppModel> data){
        this.mData = data;
        notifyDataSetChanged();
    }
    public void updateData(List<AppModel> data, int positon){
        this.mData = data;
        notifyItemChanged(positon);
    }
    public void clearData() {
        if (mData.size() != 0 && mData != null) {
            this.mData.clear();
        }
    }
    private onAddPicClickListener mOnAddPicClickListener;
    public interface onAddPicClickListener {
        void onAddPicClick(int type, int position);
    }
    public EssenceRecycleAdapter(Context context,onAddPicClickListener onAddPicClickListener) {
        this.mOnAddPicClickListener = onAddPicClickListener;
        mContext = context;

    }
    /*item点击事件*/
    private OnItemClickListener mOnItemClickListener = null;

    //define interface
    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;

            view = LayoutInflater.from(mContext).inflate(R.layout.item_app, null);
            view.setOnClickListener(this);
            return new VedioViewHodler(view);

    }
    /*绑定数据*/
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

           /*处理视频*/

            final VedioViewHodler mVedioHoder = (VedioViewHodler) holder;
            //将position保存在itemView的Tag中，以便点击时进行获取
            mVedioHoder.itemView.setTag(position);
            //图标
            String url = mData.get(position).getIconUrl();
            ImageLoader.getInstance().displayImage(url, mVedioHoder.iv_logo);
            //名字
        if(mData.get(position).getAppPackName().equals("com.anrongvs")){
            mVedioHoder.tv_name .setText("ocr扫描");

        }else{
            mVedioHoder.tv_name .setText(mData.get(position).getAppName());
        }
            //星
           mVedioHoder.mRatingBar.setRating(mData.get(position).getScore() == 0 ? 3.5f : mData.get(position).getScore());

         //点击按钮
          mVedioHoder.tv_vesion.setText(mData.get(position).getLocalVersionName());



            //点击卸载
            mVedioHoder.mButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnAddPicClickListener.onAddPicClick(0, mVedioHoder.getAdapterPosition());
                }
            });



    }



    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    public class VedioViewHodler extends RecyclerView.ViewHolder {
        ImageView iv_logo;
        TextView tv_name, tv_vesion;
        RatingBar mRatingBar;

        Button mButton;


        public VedioViewHodler(View view) {
            super(view);
             iv_logo = (ImageView) itemView.findViewById(R.id.iv_item_app_icon);
             tv_name = (TextView) itemView.findViewById(R.id.tv_item_app_name);
             tv_vesion = (TextView) itemView.findViewById(R.id.tv_item_app_version_name);
             mRatingBar = (RatingBar) itemView.findViewById(R.id.ratingbar_item_app_score);
             mButton = (Button) itemView.findViewById(R.id.btn_item_app_install);
        }

    }



}
